package controller

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo/v4"
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
	"law_case_api/services"
	"law_case_api/utils"
	"net/http"
	"strconv"
	"time"
)

// @Summary 创建案件信息
// @Tags 非诉讼案件管理
// @Accept plain
// @Produce plain
// @Param CreateCaseTask body dto.CaseTaskReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/task/create [post]
func CreateCaseTask(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}

	caseTaskReq := dto.CaseTaskReq{}
	if err := c.Bind(&caseTaskReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	if caseTaskReq.CaseId == 0 {
		resp.Code = 400
		resp.Message = "案件编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	wzjCaseTask := models.WzjCaseTask{}
	_ = utils.MapTo(caseTaskReq, &wzjCaseTask)
	wzjCaseTask.TaskCreateDate = time.Now().Unix()
	wzjCaseTask.TaskCreateUser = claims.UserId

	_case := services.CaseService{}
	if wzjCaseTask.TaskCode > 0 {
		boolRes, err := _case.UpdateCaseTask(wzjCaseTask)
		if err == nil && boolRes {
			resp.Message = "更新案件成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("更新案件任务失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "更新案件任务失败"
		return c.JSON(http.StatusOK, resp)

	} else {
		boolRes, err := _case.CreateCaseTask(wzjCaseTask)
		if err == nil && boolRes {
			resp.Message = "创建案件成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("创建案件任务失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "创建案件任务失败"
		return c.JSON(http.StatusOK, resp)
	}
}

// CaseTaskList
// @Summary 获取当前案件条件的信息
// @Tags 非诉讼案件管理
// @Accept plain
// @Produce plain
// @Param CaseTaskList body dto.CaseTaskListReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/task/list [get]
func CaseTaskList(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	caseTaskListReq := dto.CaseTaskListReq{}
	if err := c.Bind(&caseTaskListReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	_case := services.CaseService{}
	respData, respPageCount, err := _case.GetCaseTaskListByCondition(userid, caseTaskListReq)
	if err != nil {
		resp.Code = 400
		return c.JSON(http.StatusOK, resp)
	}
	resp.PageCount = respPageCount
	var data []dto.CaseTaskReq
	utils.MapTo(respData, &data)
	resp.Data = data
	return c.JSON(http.StatusOK, resp)
}

// @Summary 根据id获取合同详情
// @Tags 非诉讼案件管理
// @Accept plain
// @Produce plain
// @Param taskId query string true "案件任务id"
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/task/get [get]
func GetCaseTaskInfo(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	taskId, _ := strconv.Atoi(c.QueryParam("taskId"))
	if taskId == 0 {
		resp.Code = 400
		resp.Message = "案件任务编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	_case := services.CaseService{}

	wzjCaseTask, err := _case.GetCaseTaskById(taskId)
	if err != nil {
		resp.Code = 400
		resp.Message = "根据任务编号获取任务详情失败" + err.Error()
		return c.JSON(http.StatusOK, resp)
	}
	//格式化
	caseTaskReq := dto.CaseTaskReq{}
	_ = utils.MapTo(wzjCaseTask, &caseTaskReq)

	resp.Data = caseTaskReq
	return c.JSON(http.StatusOK, resp)
}

// @Summary 根据id获取合同详情
// @Tags 非诉讼案件管理
// @Accept plain
// @Produce plain
// @Param DelCaseTaskInfo body dto.CaseTaskReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/task/del [post]
func DelCaseTaskInfo(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	taskId, _ := strconv.Atoi(c.QueryParam("taskId"))
	if taskId == 0 {
		resp.Code = 400
		resp.Message = "案件任务编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	_case := services.CaseService{}

	wzjCaseTask, err := _case.GetCaseTaskById(taskId)
	if err != nil {
		resp.Code = 400
		resp.Message = "根据任务编号获取任务详情失败" + err.Error()
		return c.JSON(http.StatusOK, resp)
	}
	//格式化
	caseTaskReq := dto.CaseTaskReq{}
	_ = utils.MapTo(wzjCaseTask, &caseTaskReq)

	resp.Data = caseTaskReq
	return c.JSON(http.StatusOK, resp)
}
